﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using KPIS.Utils;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingCourses : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private Main main = new Main();
        HR_Development iniData = new HR_Development();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        InitialData_BLL iniDataCur = new InitialData_BLL();
        ITrainingScholarshipFundBLL SchFundBLL = new TrainingScholarshipFundBLL();
        ComboBLL combo = new ComboBLL();
        TrainingInstituteBLL InstituteBLL = new TrainingInstituteBLL();
        ConvertNullable cn = new ConvertNullable();
        TrainingCoursesInfo courseInfo = new TrainingCoursesInfo();
        TrainingCoursesDAL courseDAL = new TrainingCoursesDAL();

        int bStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
             if (!IsPostBack)
            {
                //LoadDetail();
                LoadDataGrid();
                bindCommandType();
                CBTrainingCur();
                DDLScholarFundID();
                DDLBgmSource();
                DDLTrainingGroup();
                DDLTInstitute();

            }
        }
        //protected void LoadDetail()
        //{
        //    int train_seq = Convert.ToInt32(Request.QueryString["Sequence"].ToString());
        //    TrainingCoursesInfo info = courseDAL.GetData(train_seq);
            
        //}
        private void LoadDataGrid()
        {

            RadGrid2.DataSource = courseDAL.GetFull();
            RadGrid2.DataBind();
            //DataSet ListSalaryCommand = iniData.ListScholarshipType("");
            //RadGrid2.DataSource = ListSalaryCommand.Tables[0];           
        }

        protected void RadGrid2_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                ClearControl();
                UpdateUI("AddNew");

            }
            else if (e.CommandName == "ViewDetail")
            {
               
            }
            else if (e.CommandName == "DataEdit")
            {                
                int train_seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["train_seq"].Text);
                TrainingCoursesInfo info = courseDAL.GetData(train_seq);
                UpdateUI("Update");
                txtTrainSeq.Text = train_seq.ToString();
                cbTrainingCur.SelectedValue = (info.training_cur_seq).ToString();
                txtRefNo.Text = (info.ref_no).ToString();
                //EmpPreCon
                string bStatus = (info.closed).ToString();
                if (bStatus == "1")
                { rbStatus.Checked = true; }
                else
                { rbStatus.Checked = false; }
                txtTrainingCourseDescr.Text = (info.training_course_descr).ToString();
                ddlInstitute.SelectedValue = info.training_ins_seq.ToString();
                ddlScholarFundID.SelectedValue = info.scholar_fund_seq.ToString();
                ddlBgmSource.SelectedValue = info.bg_source_seq.ToString();
                ddlTrainingGroup.SelectedValue = info.training_group_seq.ToString();
                if (info.date_sign_start.ToString().Length > 0)
                { txtDateSignStart.SetValue(info.date_sign_start.Value.ToShortDateString()); }
                if (info.date_sign_end.ToString().Length > 0)
                { txtDateSignEnd.SetValue(info.date_sign_end.Value.ToShortDateString()); }
                if (info.date_training_start.ToString().Length > 0)
                { txtDateStart.SetValue(info.date_training_start.Value.ToShortDateString()); }
                if (info.date_training_end.ToString().Length > 0)
                { txtDateEnd.SetValue(info.date_training_end.Value.ToShortDateString()); }
                if (info.time_sign_end.ToString().Length > 0)
                { txtTimeSignEnd.Text = info.time_sign_end.Substring(0, 5); }
                //txtDateSignEnd.SetValue(info.date_sign_end.ToString);
                //txtDateStart.GetDateValue;
                //txtDateEnd.GetDateValue;
                txtMaxTraineeCount.Text = info.max_trainee_count.ToString();
                txtTotalPrice.Text = info.total_price.ToString();
                txtSchedule.Text = info.schedule_text;
                ddlCountry.SelectedValue = info.country_seq.ToString();
                txtRemark.Text = info.remarks.ToString();
                //txtTotalScore.Text;
                //txtScoreVeryGood.Text;
                //txtScoreGood.Text;
                //txtScoreAcceptable.Text;
                rblScoreType.SelectedValue = info.score_type.ToString();
            }
            else if (e.CommandName == "Delete")
            {
                int train_seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["train_seq"].Text);
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;
                TrainingCoursesInfo info = new TrainingCoursesInfo();
                info.train_seq = train_seq;
                info.updated_by = UserID;
                info.updated_when = dtNow;
                courseDAL.Delete(info);
                UpdateUI("Save");
            }
            LoadDataGrid();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
           // bool sta = courseDAL.chkDupTrainingCourse(txtRefNo.Text);
           // if (sta != true)
            //{
                Nullable<int> TrainingCur = cn.ConvertNullInt(cbTrainingCur.SelectedValue);
                string strRefNo = txtRefNo.Text;
                string strCourseDescr = txtTrainingCourseDescr.Text;
                Nullable<int> Institute = cn.ConvertNullInt(ddlInstitute.SelectedValue);
                Nullable<int> cholarID = cn.ConvertNullInt(ddlScholarFundID.SelectedValue);
                Nullable<int> BgSoure = cn.ConvertNullInt(ddlBgmSource.SelectedValue);
                Nullable<int> TRGroup = cn.ConvertNullInt(ddlTrainingGroup.SelectedValue);
                int EmpPreCon = 1;
                Nullable<DateTime> DateSignStart = cn.ConvertNullDateTime(txtDateSignStart.GetStringValue());
                Nullable<DateTime> DateSignEnd = cn.ConvertNullDateTime(txtDateSignEnd.GetStringValue());
                Nullable<DateTime> DateStart = cn.ConvertNullDateTime(txtDateStart.GetStringValue());
                Nullable<DateTime> DateEnd = cn.ConvertNullDateTime(txtDateEnd.GetStringValue());
                Nullable<int> MaxTrainee = cn.ConvertNullInt(txtMaxTraineeCount.Text);
                Nullable<int> TotalPrice = cn.ConvertNullInt(txtTotalPrice.Text);
                string strSchedule = txtSchedule.Text;
                Nullable<int> Country = cn.ConvertNullInt(ddlCountry.SelectedValue);
                string strRemark = txtRemark.Text;

                if (rbStatus.Checked == true)
                { bStatus = 1; }
                else
                { bStatus = 0; }
                Nullable<int> TotalScore = cn.ConvertNullInt(txtTotalScore.Text);
                Nullable<int> ScoreVeryGood = cn.ConvertNullInt(txtScoreVeryGood.Text);
                Nullable<int> ScoreGood = cn.ConvertNullInt(txtScoreGood.Text);
                Nullable<int> ScoreAccep = cn.ConvertNullInt(txtScoreAcceptable.Text);
                int ScoreType = Convert.ToInt32(rblScoreType.SelectedValue);
                int UserID = 70;
                //int UserID = ssMng.EmpSeq;
                DateTime dtNow = DateTime.Now;
                TrainingCoursesInfo info = new TrainingCoursesInfo();

                if (!CheckNullValue())
                {
                    return;

                }

                info.train_seq = Convert.ToInt32(txtTrainSeq.Text);
                info.training_cur_seq = TrainingCur;
                info.ref_no = strRefNo;
                info.training_course_descr = strCourseDescr;
                info.training_ins_seq = Institute;
                info.scholar_fund_seq = cholarID;
                info.bg_source_seq = BgSoure;
                info.training_group_seq = TRGroup;
                info.emp_sec_seq = EmpPreCon;
                info.date_sign_start = DateSignStart;
                info.date_sign_end = DateSignEnd;
                info.date_training_start = DateStart;
                info.date_training_end = DateEnd;
                info.time_sign_end = txtTimeSignEnd.Text; // เปลี่ยนจาก .text
                info.max_trainee_count = MaxTrainee;
                info.total_price = TotalPrice;
                info.schedule_text = strSchedule;
                info.country_seq = Country;
                info.remarks = strRemark;
                info.closed = bStatus;
                info.total_score = TotalScore;
                info.score_very_good = ScoreVeryGood;
                info.score_acceptable = ScoreAccep;
                info.score_type = ScoreType;
                info.updated_by = UserID;
                info.updated_when = dtNow;
                courseDAL.Update(info);
                UpdateUI("Save");
                LoadDataGrid();
            //}
            //else
            //{
            //    Utils.Web.ShowAlert(this.Page, "เลขที่อ้างอิงที่กรอกมีในระบบแล้ว กรุณากรอกเลขที่อ้างอิงใหม่!!");
            //    return;
            //}
            //Label1.Text = "เลขที่อ้างอิงที่กรอกมีในระบบแล้ว กรุณากรอกเลขที่อ้างอิงใหม่";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool sta = courseDAL.chkDupTrainingCourse(txtRefNo.Text);
            if (sta != true)
            {
                Nullable<int> TrainingCur = cn.ConvertNullInt(cbTrainingCur.SelectedValue);
                string strRefNo = txtRefNo.Text;
                string strCourseDescr = txtTrainingCourseDescr.Text;
                Nullable<int> Institute = cn.ConvertNullInt(ddlInstitute.SelectedValue);
                Nullable<int> cholarID = cn.ConvertNullInt(ddlScholarFundID.SelectedValue);
                Nullable<int> BgSoure = cn.ConvertNullInt(ddlBgmSource.SelectedValue);
                Nullable<int> TRGroup = cn.ConvertNullInt(ddlTrainingGroup.SelectedValue);
                int EmpPreCon = 1;
                Nullable<DateTime> DateSignStart = cn.ConvertNullDateTime(txtDateSignStart.GetStringValue());
                Nullable<DateTime> DateSignEnd = cn.ConvertNullDateTime(txtDateSignEnd.GetStringValue());
                Nullable<DateTime> DateStart = cn.ConvertNullDateTime(txtDateStart.GetStringValue());
                Nullable<DateTime> DateEnd = cn.ConvertNullDateTime(txtDateEnd.GetStringValue());
                Nullable<int> MaxTrainee = cn.ConvertNullInt(txtMaxTraineeCount.Text);
                Nullable<int> TotalPrice = cn.ConvertNullInt(txtTotalPrice.Text);
                string strSchedule = txtSchedule.Text;
                Nullable<int> Country = cn.ConvertNullInt(ddlCountry.SelectedValue);
                string strRemark = txtRemark.Text;
                int bStatus;
                    if (rbStatus.Checked == true)
                    { bStatus = 1; }
                    else
                    { bStatus = 0; }
                Nullable<int> TotalScore = cn.ConvertNullInt(txtTotalScore.Text);
                Nullable<int> ScoreVeryGood = cn.ConvertNullInt(txtScoreVeryGood.Text);
                Nullable<int> ScoreGood = cn.ConvertNullInt(txtScoreGood.Text);
                Nullable<int> ScoreAccep = cn.ConvertNullInt(txtScoreAcceptable.Text);
                int ScoreType = Convert.ToInt32(rblScoreType.SelectedValue);
                int UserID = 70;
                //int UserID = ssMng.EmpSeq;
                DateTime dtNow = DateTime.Now;

                    if (!CheckNullValue())
                    {
                        return;
                        
                    }
                    TrainingCoursesInfo info = new TrainingCoursesInfo();
                    info.training_cur_seq = TrainingCur;
                    info.ref_no = strRefNo;
                    info.training_course_descr = strCourseDescr;
                    info.training_ins_seq = Institute;
                    info.scholar_fund_seq = cholarID;
                    info.bg_source_seq = BgSoure;
                    info.training_group_seq = TRGroup;
                    info.emp_sec_seq = EmpPreCon;
                    info.date_sign_start = DateSignStart;
                    info.date_sign_end = DateSignEnd;
                    info.date_training_start = DateStart;
                    info.date_training_end = DateEnd;
                    info.max_trainee_count = MaxTrainee;
                    info.total_price = TotalPrice;
                    info.schedule_text = strSchedule;
                    info.country_seq = Country;
                    info.remarks = strRemark;
                    info.closed = bStatus;
                    info.total_score = TotalScore;
                    info.score_very_good = ScoreVeryGood;
                    info.score_acceptable = ScoreAccep;
                    info.score_type = ScoreType;
                    info.created_by = UserID;
                    info.created_when = dtNow;
                    courseDAL.Insert(info);
                    UpdateUI("Save");
                    LoadDataGrid();
                
            }
            Label1.Text = "เลขที่อ้างอิงที่กรอกมีในระบบแล้ว กรุณากรอกเลขที่อ้างอิงใหม่";
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            cbTrainingCur.SelectedIndex = 0;
            txtRefNo.Text = "";
            txtTrainingCourseDescr.Text = "";
            ddlInstitute.SelectedIndex = 0;
            ddlScholarFundID.SelectedIndex = 0;
            ddlBgmSource.SelectedIndex = 0;
            ddlTrainingGroup.SelectedIndex = 0;
            txtEmpPreCon.Text = "";
            txtMaxTraineeCount.Text = "";
            txtTotalPrice.Text = "";
            txtSchedule.Text = "";
            ddlCountry.SelectedIndex = 0;
            rblScoreType.SelectedIndex = 0;
            txtRemark.Text = "";
            //rblCoutry.SelectedValue = "1";
            Label1.Text = "";
            //txtScholaType.Text = "";
            //txtScholaTypeSeq.Text = "";
        }

        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnUpdate.Visible = false;
                    btnSave.Visible = true;

                    break;
                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }
        protected void DDLBgmSource()
        {

            ddlBgmSource.DataSource = iniDataCur.ListBgmSource("");
            ddlBgmSource.DataBind();
        }

        protected void DDLScholarFundID()
        {
            ddlScholarFundID.DataSource = SchFundBLL.GetFull();
            ddlScholarFundID.DataBind();
        }
        private void CBTrainingCur()
        {
            cbTrainingCur.DataSource = iniDataCur.ListTrainingCurriculums("");
            cbTrainingCur.DataBind();
        }

        private void bindCommandType()
        {
            DataTable InfoList = combo.ListCountries();

            ddlCountry.DataSource = InfoList;
            ddlCountry.DataBind();
        }
        private void DDLTrainingGroup()
        {
            ddlTrainingGroup.DataSource = iniDataCur.ListTrainingGroup("");
            ddlTrainingGroup.DataBind();
        }
        private void DDLTInstitute()
        {
            ddlInstitute.DataSource = InstituteBLL.GetFull();
            ddlInstitute.DataBind();
        }

        private bool CheckNullValue() //Edit By Mai
        {
            bool bOk = true;

            if (cbTrainingCur.SelectedValue.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกหลักสูตรอบรม");
                bOk = false;
            }
            else if (txtRefNo.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกเลขที่อ้างอิง");
                bOk = false;
            }
            else if (txtTrainingCourseDescr.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกรายละเอียดการอบรม");
                bOk = false;
            }
            else if (ddlInstitute.SelectedValue.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกหน่วยงานที่จัดอบรม");
                bOk = false;
            }
            else if (ddlScholarFundID.SelectedValue.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกแหล่งทุน");
                bOk = false;
            }
            else if (ddlBgmSource.Text.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกแหล่งเงิน");
                bOk = false;
            }
            else if (ddlTrainingGroup.Text.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกกลุ่มการอบรม");
                bOk = false;
            }
            else if (txtSchedule.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกสถานที่อบรม");
                bOk = false;
            }
            else if (ddlCountry.Text.Length == 0)
            {
                main.MessageAlert(Page, "กรุณาเลือกข้อมูลหลักสูตรอบรม");
                bOk = false;
            }

            return bOk;
        }

    }
}
